package org.example.mine.service.impl;

import cn.hutool.core.util.IdUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.mine.config.LocalDateTimeUtil;
import org.example.mine.mapper.TopUpMapper;
import org.example.mine.service.TopUpService;
import org.example.model.domain.express.ExpressDetial;
import org.example.model.domain.mine.ExpressExchange;
import org.example.model.domain.mine.ExpressPaymentRecords;
import org.example.model.domain.mine.ExpressRechargeRecord;
import org.example.model.vo.mine.TopUpVo;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TopUpServiceImpl implements TopUpService {


    @Resource
    public TopUpMapper topUpMapper;
    @Override
    public TopUpVo selectTopUpList(Long registerIp) {
        List<ExpressRechargeRecord> expressRechargeRecord = topUpMapper.selectTopUpList(registerIp);
        TopUpVo topUpVo = new TopUpVo();
        if(expressRechargeRecord.size()!=0){
//            使用stream流中的Collectors.groupingBy进行分组排序
            LinkedHashMap<String, List<ExpressRechargeRecord>> map = expressRechargeRecord.stream().sorted(Comparator.comparing(ExpressRechargeRecord::getCreateTime).reversed()).collect(Collectors.groupingBy(e-> LocalDateTimeUtil.parse_yyyyMM(e.getCreateTime()),LinkedHashMap::new,Collectors.toList()));
            log.info("map"+map);
            topUpVo.setMap(map);
        }
        return topUpVo;
    }

    @Override
    public ExpressRechargeRecord gettopupbyid(Long id) {
        return topUpMapper.gettopupbyid(id);
    }

    @Override
    public ExpressExchange getexchange() {
        return topUpMapper.getexchange();
    }

    @Override
    public int addTopupOrder(ExpressRechargeRecord expressRechargeRecord) {
        String s = IdUtil.getSnowflakeNextIdStr();
        expressRechargeRecord.setTransactionNumber(s);
        expressRechargeRecord.setCreateTime(new Date());
        expressRechargeRecord.setStatus(2);
        int num=topUpMapper.addTopupOrder(expressRechargeRecord);
        return num;
    }

    @Override
    public void topupfail(Integer id) {
        ExpressRechargeRecord expressRechargeRecord = new ExpressRechargeRecord();
        expressRechargeRecord.setId(id);
        expressRechargeRecord.setOverTime(new Date());
        expressRechargeRecord.setStatus(5);
        expressRechargeRecord.setFailureReason("充值接口未调用");
        expressRechargeRecord.setClosingTime(new Date());
        topUpMapper.topupfail(expressRechargeRecord);
    }

    @Override
    public void alipaycallback(ExpressRechargeRecord expressRechargeRecord) {
        topUpMapper.alipaycallback(expressRechargeRecord);
    }

    @Override
    public void updateUserBalance(ExpressDetial expressDetial) {
        log.info("ExpressDetial expressDetial"+ expressDetial);
        topUpMapper.updateUserBalance(expressDetial);
    }

    @Override
    public void addPayOrder(ExpressRechargeRecord expressRechargeRecord) {
        ExpressPaymentRecords expressPaymentRecords = new ExpressPaymentRecords();
        String s = IdUtil.getSnowflakeNextIdStr();
        expressPaymentRecords.setTransactionNumber(s);
        expressPaymentRecords.setType(1);
        expressPaymentRecords.setPhone(expressRechargeRecord.getPhone());
        expressPaymentRecords.setOrderType(1);
        expressPaymentRecords.setOrderNo(s);
        expressPaymentRecords.setPayableAmount(new BigDecimal(expressRechargeRecord.getRechargeAmount()));
        expressPaymentRecords.setPaymentChannels(expressRechargeRecord.getRechargeChannels());
        expressPaymentRecords.setPaymentCurrency(expressRechargeRecord.getRechargeCurrency());
        expressPaymentRecords.setActualPayment(new BigDecimal(expressRechargeRecord.getActualPayment()));
        expressPaymentRecords.setExchangeRate(new BigDecimal(expressRechargeRecord.getExchangeRate()));
        expressPaymentRecords.setCreateTime(new Date());
        expressPaymentRecords.setOverTime(new Date());
        expressPaymentRecords.setStatus(2);
        expressPaymentRecords.setDeliveryFee(new BigDecimal(expressRechargeRecord.getActualPayment()));
        expressPaymentRecords.setDiscountAmount(new BigDecimal(0));
        expressPaymentRecords.setPaymentAccount(expressRechargeRecord.getRegisterIp());
        topUpMapper.addPayOrder(expressPaymentRecords);
    }

    @Override
    public ExpressDetial getexpressdetail(String id) {
        return topUpMapper.getexpressdetail(id);
    }

    @Override
    public ExpressRechargeRecord selectorder(String transactionNumber) {
        return topUpMapper.selectorder(transactionNumber);
    }

    @Override
    public void topupfila(Integer id) {
        Date date = new Date();
        topUpMapper.topupfila(id,date);
    }
}
